//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.yaukie.base.core.controller;

import java.beans.PropertyEditorSupport;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yaukie.base.constant.BaseResult;
import org.yaukie.base.util.DateUtils;
import org.yaukie.base.util.StringTools;

public class BaseController {
    private static final Logger log = LoggerFactory.getLogger(BaseController.class);
    public static final String RESULT_ROWS = "rows";
    public static final String RESULT_TOTLAL = "total";

    public BaseController() {
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            public void setAsText(String text) {
                this.setValue(DateUtils.parseDate(text));
            }
        });
    }

    @ExceptionHandler
    @ResponseBody
    public Object exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) {
        log.error("未捕获的异常：", exception);
        return new BaseResult(0, "未捕获的异常", "异常详细信息请查看日志");
    }

    protected BaseResult toAjax(int rows) {
        return rows > 0 ? BaseResult.success() : BaseResult.fail();
    }

    protected BaseResult toAjax(boolean result) {
        return result ? this.success() : this.error();
    }

    public BaseResult success() {
        return BaseResult.success();
    }

    public BaseResult error() {
        return BaseResult.fail();
    }

    public BaseResult success(String message) {
        return BaseResult.success(message);
    }

    public BaseResult error(String message) {
        return BaseResult.fail(message);
    }

    public String redirect(String url) {
        return StringTools.format("redirect:{}", new Object[]{url});
    }
}
